<?php if (!defined('BASEPATH'))exit('No direct script access allowed');

class Jadwal extends CI_Controller
{
    public function index()
    {
		$data['select_tahun'] = $this->db->from('tahun')->order_by('id_tahun', 'ASC')->get();
		$data['id_tahun'] = $this->db->select('id_tahun')->from('tahun')->where('tahun_aktif', '1')->get()->row()->id_tahun;
                $data['id_hari'] = '';
                $data['id_kelas'] = '';
        $this->template->admin('admin/v_jadwal', $data, 3, "Jadwal Pelajaran");
    }
	
	public function tahun($tahun = '', $kelas = '', $hari = '')
	{
		$id_tahun = $this->encrypt->decode($this->input->post('id_tahun'));
		$id_kelas = $this->encrypt->decode($this->input->post('id_kelas'));
		$id_hari = $this->input->post('hari');
		if($id_tahun == '' AND $tahun != '') $id_tahun = $tahun;
		if($id_kelas == '' AND $kelas != '') $id_kelas = $kelas;
		if($id_hari == '' AND $hari != '') $id_hari = $hari;
		if($id_tahun == '' OR $id_kelas == '' OR $id_hari == '') show_404();
		else
		{
			$data_tahun = $this->db->select('tahun_angkatan')->from('tahun')->where('id_tahun', $id_tahun)->get();
			if($data_tahun->num_rows() > 0)
			{
				$data_tahun = $data_tahun->row();
				$data['id_tahun'] = $id_tahun;
				$data['id_kelas'] = $id_kelas;
				$data['id_hari'] = $id_hari;
				$data['tahun_pelajaran'] = $data_tahun->tahun_angkatan;
				$data['data_jadwal'] = $this->db->select('j.id_jadwal, j.jam_pelajaran, jam_pelajaran(j.jam_pelajaran) as waktu_pelajaran, p.nama_pelajaran, g.nama_guru, j.istirahat, ruangan.nama_kelas AS ruangan')
                                                                ->from('jadwal_pelajaran j')
                                                                ->join('data_kelas k', 'k.id_kelas=j.id_kelas', 'INNER')
                                                                ->join('data_kelas ruangan', 'ruangan.id_kelas=j.ruangan_kelas', 'LEFT')
                                                                ->join('mata_pelajaran m', 'm.id=j.id_mapel', 'LEFT')
                                                                ->join('tabel_pelajaran p', 'p.kode_pelajaran=m.kode_pelajaran', 'LEFT')
                                                                ->join('data_guru g', 'g.id=m.kode_guru', 'LEFT')
                                                                ->where('j.id_tahun', $id_tahun)
                                                                ->where('j.id_kelas', $id_kelas)
                                                                ->where('j.hari', $id_hari)
                                                                ->group_by('j.id_jadwal')
                                                                ->order_by('j.id_tahun', 'DESC')
                                                                ->order_by('j.hari', 'ASC')
                                                                ->order_by('j.jam_pelajaran', 'ASC')
                                                                ->get();
                                
				$data['nama_kelas'] = $this->db->select('nama_kelas')->from('data_kelas')->where('id_kelas', $id_kelas)->get()->row()->nama_kelas;
				$data['nama_hari'] = hari_indo($id_hari);
				if($tahun != '')
				{
					$data['select_tahun'] = $this->db->from('tahun')->order_by('id_tahun', 'ASC')->get();
					$this->template->admin('admin/v_jadwal', $data, 3, "Jadwal Pelajaran");
				}
				else
				{
					$this->load->view('admin/v_jadwal_data', $data);
				}
			}
			else
			{
				show_404();
			}
		}
	}
	
	public function tambah($id_tahun = null, $id_kelas = null, $id_hari = null)
	{
		if($id_tahun == null || $id_kelas == null || $id_hari == null)
		{
			show_404();
		}
		else
		{
			$this->load->library('form_validation');
			$this->_rule_validation();
			if ($this->form_validation->run() === FALSE)
			{
				
				$data['id'] = '';
				$data['id_tahun'] = $id_tahun;
				$data['id_kelas'] = $id_kelas;
				$data['id_hari'] = $id_hari;
				$data['nama_hari'] = hari_indo($id_hari);
				$data['nama_kelas'] = $this->db->select('nama_kelas')->from('data_kelas k')->where('k.id_tahun', $id_tahun)->where('k.id_kelas', $id_kelas)->order_by('k.nama_kelas', 'ASC')->get()->row()->nama_kelas;
				$this->template->admin('admin/v_jadwal_form', $data, 3, "Tambah Jadwal Pelajaran ".$this->db->select('tahun_angkatan')->from('tahun')->where('id_tahun', $id_tahun)->get()->row()->tahun_angkatan);
			}
			else
			{
				$this->_simpan('TAMBAH');
			}
		}
	}
	
	private function _simpan($proses = null)
	{
		if($proses == null) show_404();
		else
		{
			$id_tahun = $this->input->post('id_tahun');
			$id_kelas = $this->input->post('id_kelas');
			$id_mapel = $this->input->post('id_mapel');
			$hari = $this->input->post('id_hari');
			$jam_pelajaran = $this->input->post('jam_pelajaran');
			$id_ruangan = $this->input->post('id_ruangan');
			$datasimpan = array(
				'id_tahun' => $id_tahun,
				'id_kelas' => $id_kelas,
				'id_mapel' => $id_mapel,
				'hari' => $hari,
				'jam_pelajaran' => $jam_pelajaran,
				'ruangan_kelas' => $id_ruangan,
				'istirahat' => 0
			);
			$this->db->insert('jadwal_pelajaran', $datasimpan);
                        if($this->input->post('lagi') == '1')
                        {
                            redirect('admin/jadwal/tambah/'.$id_tahun.'/'.$id_kelas.'/'.$hari);
                        }
                        else
                        {
                            redirect('admin/jadwal/tahun/'.$id_tahun.'/'.$id_kelas.'/'.$hari);
                        }
		}
	}
	
	public function hapus($id_jadwal = null)
	{
		if($id_jadwal == null) show_404();
		else
		{
			$r_jadwal = $this->db->select('id_tahun, id_kelas, hari')->from('jadwal_pelajaran')->where('id_jadwal', $id_jadwal)->get();
			if($r_jadwal->num_rows() > 0)
			{
				$this->db
				->where('id_jadwal', $id_jadwal)
				->delete('jadwal_pelajaran');
                                redirect('admin/jadwal/tahun/'.$r_jadwal->row()->id_tahun.'/'.$r_jadwal->row()->id_kelas.'/'.$r_jadwal->row()->hari);
			}
			else show_404();
		}
	}
	
	private function _rule_validation()
	{
		$this->form_validation->set_message('required', '%s harap di isi');
//		$this->form_validation->set_rules('hari', 'Hari', 'required|is_natural_no_zero|trim');
//		$this->form_validation->set_rules('id_kelas', 'Kelas', 'required|is_natural_no_zero|trim');
		$this->form_validation->set_rules('jam_pelajaran', 'Jam Pelajaran', 'required|is_natural_no_zero|trim');
		$this->form_validation->set_rules('id_mapel', 'Mata Pelajaran', 'required|is_natural_no_zero|trim');
		$this->form_validation->set_rules('id_ruangan', 'Ruangan', 'required|is_natural_no_zero|trim');
		$this->form_validation->set_rules('lagi', '', '');
	}
	
	public function kelas()
	{
		$id_tahun = $this->input->post('id_tahun');
		$hari = $this->input->post('hari');
		$id_kelas = $this->input->post('id_kelas');
		// $id_tahun = 3;
		// $hari = 1;
		// $id_kelas = 1;
		if($id_tahun == '') show_404();
		if($id_kelas != '')
		{
			$rslt_jam = $this->db->distinct()->select('jam_pelajaran')->from('jadwal_pelajaran')->where('id_tahun', $id_tahun)->where('hari', $hari)->where('id_kelas', $id_kelas)->or_where('id_kelas', null)->order_by('jam_pelajaran', 'ASC')->get();
			echo '<select id="jam_pelajaran" name="jam_pelajaran" class="uniformselect2">';
			echo '<option value="">-Pilih-</option>';
			$j = 1;
                        $total_jam_pelajaran = 6;
                        if($hari == 5) $total_jam_pelajaran = 4;
			while($j <= $total_jam_pelajaran)
			{
				$cek = true;
				foreach($rslt_jam->result() as $v_jam)
				{
					if($v_jam->jam_pelajaran == $j) $cek = false;
				}
                                if($hari == 1 AND $j == 1 AND $cek == true)
                                    echo '<option value="'.$j.'" disabled>'.$j.' - Upacara</option>';
				elseif($cek == true)
				{
                                    echo '<option value="'.$j.'">'.$j.'</option>';
				}
				$j++;
			}
			echo '</select>';
			$this->load->view('admin/submit/sub_jam');
		}
		else
		{
			echo '<select id="jam_pelajaran" name="jam_pelajaran" class="uniformselect2">';
			echo '<option value="">Harap Pilih Kelas Terlebih Dahulu</option>';
			echo '</select>';
		}
	}
	
	public function jam()
	{
		$id_tahun = $this->input->post('id_tahun');
		$hari = $this->input->post('hari');
		$id_kelas = $this->input->post('id_kelas');
		$jam_pelajaran = $this->input->post('jam_pelajaran');
		if($id_tahun == '') show_404();
		if($jam_pelajaran != '')
		{
			$rslt_pelajaran = $this->db->query('SELECT m.id, p.nama_pelajaran, g.nama_guru FROM mata_pelajaran m
INNER JOIN tabel_pelajaran p ON(m.kode_pelajaran=p.kode_pelajaran)
INNER JOIN data_guru g ON(g.id=m.kode_guru)
LEFT JOIN jadwal_pelajaran j ON(j.id_mapel = m.id AND j.id_tahun = '.$id_tahun.' AND j.hari = '.$hari.' AND j.jam_pelajaran = '.$jam_pelajaran.')
WHERE j.id_jadwal IS NULL
GROUP BY m.id
ORDER BY p.nama_pelajaran ASC');
			echo '<select id="id_mapel" name="id_mapel" class="uniformselect3">';
			echo '<option value="">-Pilih-</option>';
			foreach($rslt_pelajaran->result() as $v_pel)
			{
				echo '<option value="'.$v_pel->id.'">'.$v_pel->nama_pelajaran.' | '.$v_pel->nama_guru.'</option>';
			}
			echo '</select>';
		}
		else
		{
			echo '<select id="id_mapel" name="id_mapel" class="uniformselect3" disabled>';
			echo '<option value="">Harap Pilih Jam Pelajaran Terlebih Dahulu</option>';
			echo '</select>';
		}
                echo "<script>if(jQuery().uniform)jQuery('select.uniformselect3').uniform();</script>";
	}
        
        public function ruangan()
        {
		$id_tahun = $this->input->post('id_tahun');
		$hari = $this->input->post('hari');
		$id_kelas = $this->input->post('id_kelas');
		$jam_pelajaran = $this->input->post('jam_pelajaran');
		if($id_tahun == '') show_404();
		if($jam_pelajaran != '')
		{
			$rslt_pelajaran = $this->db->query('SELECT r.id_kelas, r.nama_kelas FROM data_kelas r
LEFT JOIN jadwal_pelajaran jp ON(jp.ruangan_kelas=r.id_kelas 
AND jp.jam_pelajaran='.$jam_pelajaran.' AND jp.hari='.$hari.'
)
WHERE r.id_tahun IS NULL
AND r.id_kelstat IS NULL
AND jp.id_jadwal IS NULL
GROUP BY r.id_kelas
ORDER BY r.nama_kelas');
			echo '<select id="id_ruangan" name="id_ruangan" class="uniformselect4">';
			echo '<option value="">-Pilih-</option>';
			foreach($rslt_pelajaran->result() as $v_pel)
			{
				echo '<option value="'.$v_pel->id_kelas.'">'.$v_pel->nama_kelas.'</option>';
			}
			echo '</select>';
		}
		else
		{
			echo '<select id="id_mapel" name="id_mapel" class="uniformselect4" disabled>';
			echo '<option value="">Harap Pilih Jam Pelajaran Terlebih Dahulu</option>';
			echo '</select>';
		}
                echo "<script>if(jQuery().uniform)jQuery('select.uniformselect4').uniform();</script>";
            
        }
        
}

/* End of file jadwal.php */
/* Location: ./application/controllers/admin/jadwal.php */